
//定义接口
interface NameInfo {
    firstName:string,
    lastName:string
}

/*
* const getFullName = ({firstName,lastName}:{fistName:string,lastName:string}):string=>{
    return  `${firstName} ${lastName}`;
}
* */
/*
const getFullName1 = ({firstName,lastName}:{firstName:string,lastName:string}):string=>{
    return  `${firstName} ${lastName}`;
}
*/
const getFullName = ({firstName,lastName}:NameInfo):string=>{
    return  `${firstName} ${lastName}`;
}

console.log(getFullName({firstName:'Break',lastName:'666'}))
/*
console.log(getFullName1({
    firstName:"222",
}))*/


//可选参数
/*
* 解决 参数多余的方案
*
*   1、类型断言 指定传过去的参数 就是 接口类型
*   2、索引签名
*   3、类型兼容性
*
* */
interface Vegetables{
    color?:string,
    readonly type:string,   //只读属性
    // [prop:string]:any   //索引签名
}

const getVegetables = ( {color,type}:Vegetables ):void=>{
    console.log(`A ${color ? (color+' ') : '' } ${type}`)
}
//A red  tomato

let props = {
    color:'red',
    type:'tomato',
    dd:52,
    scc:22123
}

getVegetables(props);
//A  watermelon
getVegetables({
    type:'watermelon'
});

//定义数组
interface ArrInter {
    0:number,
    readonly 1:string   //数组只读元素
}
let arr4:ArrInter = [14,';s',2];
// arr4[1] = 'ss'; // Cannot assign to '1' because it is a read-only property.

//定义函数
interface AddFunc {
    (n1:number,n2:number) : number;
}
const add:AddFunc = (n1,n2) => n1 + n2;
console.log(add(1,2));

//索引类型
interface RoleDic {
    [ids:number]:string //任意多的属性 属性key是 number 类型 值是string 类型
}
const role1:RoleDic = { //如果key是 数值的话 js会先转换成字符串
    1:'s'
}
console.log(role1);

//接口的继承

interface animal {
    age:number
}
interface dog extends animal{
    type:string
}

interface cat extends animal{
    eat:string
}
let dog1:dog = {
    type:"哈士奇",
    age:5
}
let cat1:cat = {
    eat:"猫粮",
    age:2
}
//混合类型接口
interface Counter {
    ():void,
    count:number
}

const getCountNum = ():Counter =>{
    const c:Counter = ()=>{c.count++};
    c.count = 0;
    return  c;
}

const get1 = getCountNum();
get1();
console.log(get1.count);    // 1
get1();
console.log(get1.count);    // 2
get1();
console.log(get1.count);    // 3
get1();
console.log(get1.count);    // 4